package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhijian.medical.dao.local.FishReportMapper;
import com.zhijian.medical.entity.po.ExperimentFishReport;
import com.zhijian.medical.entity.vo.request.ExperimentReportSearchVo;
import com.zhijian.medical.entity.vo.response.ExperimentReportListVo;
import com.zhijian.medical.service.FishReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author li xiao dong
 * @since 2022-08-14
 */
@Service
public class FishReportServiceImpl extends ServiceImpl<FishReportMapper, ExperimentFishReport> implements FishReportService {

    @Autowired
    private FishReportMapper reportMapper;

    @Override
    public Boolean update(ExperimentFishReport experimentFishReport) {
        if (experimentFishReport.getId() == null) {
            return create(experimentFishReport);
        }
        return reportMapper.updateById(experimentFishReport) > 0;
    }

    @Override
    public ExperimentFishReport newest() {
        QueryWrapper<ExperimentFishReport> experimentFishReportQueryWrapper = new QueryWrapper<>();
        experimentFishReportQueryWrapper.orderByDesc("id");
        experimentFishReportQueryWrapper.last("limit 1");
        return reportMapper.selectOne(experimentFishReportQueryWrapper);

    }

    @Override
    public ExperimentFishReport detail(Long id) {
        return reportMapper.selectById(id);
    }

    @Override
    public List<ExperimentReportListVo> list(ExperimentReportSearchVo experimentReportSearchVo) {
        List<ExperimentReportListVo> experimentReportListVos = reportMapper.list(experimentReportSearchVo);
        if (CollectionUtil.isEmpty(experimentReportListVos)) {
            return Collections.emptyList();
        }
        return experimentReportListVos.stream().peek(s -> s.setMainType(experimentReportSearchVo.getMainType())).collect(Collectors.toList());


    }

    @Override
    public Boolean create(ExperimentFishReport experimentFishReport) {
        return reportMapper.insert(experimentFishReport) > 0;
    }
}
